import { defineStore } from "pinia";
import { Names } from "./store-name";
import { useRequest } from "alova";
import { addMenuAction, deleteMenuAction, queryMenuActionList } from "@/api/menuAction";
import type { TCreateMenuActionReq, TMenuActionItem, TMenuActionListRes, TMethodsList } from "@/typing/menuAction";

export const useMenuActionStore = defineStore(Names.MENU_ACTION, {
  state() {
    return {
      isLoading: false,
      menuActionList: <TMenuActionItem[]>[],
      methodList: <TMethodsList>[]
    }
  },
  getters: {

  },
  actions: {
    queryMenuActionList(id: string, { force }: { force?: boolean } = { force: false }) {
      this.isLoading = true
      const { onSuccess } = useRequest(queryMenuActionList(id), { force: force });
      onSuccess(({ data }) => {
        const { data: { menuActionList, methodList } } = data as TMenuActionListRes
        this.menuActionList = menuActionList;
        this.methodList = methodList
        this.isLoading = false
      });
    },
    addMenuAction(req: TCreateMenuActionReq, fn: Function) {
      const { onSuccess } = useRequest(addMenuAction(req));
      onSuccess(({ data }) => {
        if (data) {
          fn && fn(data);
        }
      });
    },
    deleteMenuAction(id: string, fn: Function) {
      const { onSuccess } = useRequest(deleteMenuAction(id));
      onSuccess(({ data }) => {
        if (data) {
          fn && fn(data);
        }
      });
    },
  }
})
